package com.ekuaitu.kuaitu.map;

import android.content.Context;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.util.Log;
import android.widget.Toast;

import com.amap.api.maps.AMap;
import com.amap.api.maps.model.LatLng;
import com.amap.api.navi.AMapNavi;
import com.amap.api.navi.AMapNaviListener;
import com.amap.api.navi.model.AMapLaneInfo;
import com.amap.api.navi.model.AMapNaviCross;
import com.amap.api.navi.model.AMapNaviInfo;
import com.amap.api.navi.model.AMapNaviLocation;
import com.amap.api.navi.model.AMapNaviPath;
import com.amap.api.navi.model.AMapNaviStaticInfo;
import com.amap.api.navi.model.AMapNaviTrafficFacilityInfo;
import com.amap.api.navi.model.AimLessModeCongestionInfo;
import com.amap.api.navi.model.AimLessModeStat;
import com.amap.api.navi.model.NaviInfo;
import com.amap.api.navi.model.NaviLatLng;
import com.amap.api.navi.model.RouteOverlayOptions;
import com.amap.api.navi.view.RouteOverLay;
import com.autonavi.tbt.NaviStaticInfo;
import com.autonavi.tbt.TrafficFacilityInfo;
import com.ekuaitu.kuaitu.bean.RouteEvent;
import com.ekuaitu.kuaitu.utils.Toast.ToastCompat;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by Michael Pi on 2016/9/6.
 */
public class NaviUtils implements AMapNaviListener {
    private static NaviUtils instance;
    private AMapNavi aMapNavi;
    private RouteOverLay mRouteOverLay;
    private NaviLatLng curNaviPosition;
    private NaviLatLng tarNaviPosition;

    public static NaviUtils newInstance() {
        if (instance == null) {
            instance = new NaviUtils();
        }
        return instance;
    }

    //规划步行路线
    public void calculateFootRoute(Context mContext, AMap aMap, LatLng curPosition, LatLng tarPosition) {
        //获取AMapNavi实例
        aMapNavi = AMapNavi.getInstance(mContext);
        //添加监听回调，用于处理算路成功
        aMapNavi.addAMapNaviListener(this);
        if (mRouteOverLay != null) {
            mRouteOverLay.removeFromMap();
        }
        mRouteOverLay = new RouteOverLay(aMap, null, mContext);
        curNaviPosition = new NaviLatLng(curPosition.latitude, curPosition.longitude);
        tarNaviPosition = new NaviLatLng(tarPosition.latitude, tarPosition.longitude);
        boolean isSuccess = aMapNavi.calculateWalkRoute(curNaviPosition, tarNaviPosition);
        if (!isSuccess) {
            ToastCompat.makeText(mContext, "路线计算失败", Toast.LENGTH_SHORT).show();
        }
    }


    public void clearRoute(Context mContext, AMap aMap) {
        mRouteOverLay = new RouteOverLay(aMap, null, mContext);
        mRouteOverLay.removeFromMap();
    }

    //步行路线规划成功后
    @Override
    public void onCalculateRouteSuccess() {
        int dis = -1;
        int dur = -1;
        mRouteOverLay.removeFromMap();
        AMapNaviPath naviPath = aMapNavi.getNaviPath();
        // 获取路径规划线路，显示到地图上
        mRouteOverLay.setAMapNaviPath(naviPath);
        mRouteOverLay.addToMap();
        mRouteOverLay.zoomToSpan();
        dur = naviPath.getAllTime() / 60;
        dis = naviPath.getAllLength();
        //将接收到的结果传递至MainActivity
        EventBus.getDefault().post(new RouteEvent(dis, dur));
    }


    @Override
    public void onInitNaviFailure() {

    }

    @Override
    public void onInitNaviSuccess() {

    }

    @Override
    public void onStartNavi(int i) {

    }

    @Override
    public void onTrafficStatusUpdate() {

    }

    @Override
    public void onLocationChange(AMapNaviLocation aMapNaviLocation) {

    }

    @Override
    public void onGetNavigationText(int i, String s) {

    }

    @Override
    public void onEndEmulatorNavi() {

    }

    @Override
    public void onArriveDestination() {

    }

    @Override
    public void onArriveDestination(NaviStaticInfo naviStaticInfo) {

    }

    @Override
    public void onArriveDestination(AMapNaviStaticInfo aMapNaviStaticInfo) {

    }


    @Override
    public void onCalculateRouteFailure(int i) {

    }

    @Override
    public void onReCalculateRouteForYaw() {

    }

    @Override
    public void onReCalculateRouteForTrafficJam() {

    }

    @Override
    public void onArrivedWayPoint(int i) {

    }

    @Override
    public void onGpsOpenStatus(boolean b) {

    }

    @Override
    public void onNaviInfoUpdated(AMapNaviInfo aMapNaviInfo) {

    }

    @Override
    public void onNaviInfoUpdate(NaviInfo naviInfo) {

    }

    @Override
    public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo aMapNaviTrafficFacilityInfo) {

    }

    @Override
    public void OnUpdateTrafficFacility(TrafficFacilityInfo trafficFacilityInfo) {

    }

    @Override
    public void showCross(AMapNaviCross aMapNaviCross) {

    }

    @Override
    public void hideCross() {

    }

    @Override
    public void showLaneInfo(AMapLaneInfo[] aMapLaneInfos, byte[] bytes, byte[] bytes1) {

    }

    @Override
    public void hideLaneInfo() {

    }

    @Override
    public void onCalculateMultipleRoutesSuccess(int[] ints) {

    }

    @Override
    public void notifyParallelRoad(int i) {

    }

    @Override
    public void OnUpdateTrafficFacility(AMapNaviTrafficFacilityInfo[] aMapNaviTrafficFacilityInfos) {

    }

    @Override
    public void updateAimlessModeStatistics(AimLessModeStat aimLessModeStat) {

    }

    @Override
    public void updateAimlessModeCongestionInfo(AimLessModeCongestionInfo aimLessModeCongestionInfo) {

    }
}
